package class6;

import java.util.ArrayList;
import java.util.List;

public class demo1 {
    public static void main(String[] args) {

        List<String>list=new ArrayList<>();
        //获取当前时间
        long startTime=System.currentTimeMillis();
        //生产者线程
        Thread t1=new Thread(()->{
            int num=0;
            while(System.currentTimeMillis()-startTime<100){
                synchronized (list){
                        try {
                            if(list.size()>0){
                            list.wait();
                            }else {
                                list.add("商品"+(++num));
                                System.out.println("生成商品"+num);
                            }
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                }
            }
        });

        //消费者线程
        Thread t2=new Thread(()->{
            int num=0;
            while(System.currentTimeMillis()-startTime<100){
                synchronized (list){
                    if(list.size()==0){
                    list.notify();
                    }else {
                        list.remove("商品"+(++num));
                        System.out.println("消费商品"+num);
                    }
                }
            }
        });

        //启动
        t1.start();
        t2.start();


    }
}
